/**
 * 打印功能
 * @param el 要打印的元素
 * @param options 打印配置选项
 */
export const useVueprint = (el: HTMLElement, options = {}) => {
	// 默认配置
	const defaultOptions = {
		mode: 'normal', // 'normal' 或 'receipt'（小票模式）
		receiptWidth: '80mm', // 小票宽度，通常为58mm或80mm
		showPrintDialog: true, // 是否显示打印对话框
		removeAfterPrint: true, // 打印后是否移除iframe
		customStyles: '', // 自定义样式
	};

	// 合并配置
	const config = { ...defaultOptions, ...options };
	const content = el.innerHTML;
	const iframe = document.createElement('iframe');

	iframe.style.display = 'none';
	document.body.appendChild(iframe);

	const printWindow = iframe.contentWindow;
	const doc = iframe.contentDocument || printWindow?.document;

	if (!doc) return;

	// 根据模式选择不同的样式
	const modeStyles =
		config.mode === 'receipt'
			? `
      @page {
        size: ${config.receiptWidth} auto;
        margin: 0;
      }
      body {
        width: ${config.receiptWidth};
        margin: 0;
        padding: 0;
        font-family: SimSun, "宋体", Arial, sans-serif;
        font-size: 12px;
      }
      .print-page {
        width: 100%;
        padding: 2mm;
        box-sizing: border-box;
        border: 1px solid #000;
      }
      .company-title {
        text-align: center;
        font-size: 14px;
        font-weight: bold;
        border-bottom: 1px solid #000;
        padding: 2mm 0;
      }
      .receipt-title {
        text-align: center;
        font-size: 13px;
        font-weight: bold;
        border-bottom: 1px solid #000;
        padding: 2mm 0;
      }
      .content-row {
        display: flex;
        border-bottom: 1px solid #eee;
        padding: 1mm 0;
      }
      .content-label {
        min-width: 25mm;
        font-weight: normal;
      }
      .content-value {
        flex: 1;
      }
      .receipt-no {
        text-align: right;
        color: #f00;
        padding-right: 2mm;
        border-bottom: 1px solid #000;
      }
      hr {
        border: none;
        border-top: 1px solid #000;
        margin: 2mm 0;
      }
    `
			: `
      body {
        padding: 0;
        margin: 0;
        font-family: SimSun, "宋体", Arial, sans-serif;
        font-size: 14px;
      }
      .print-page {
        page-break-after: always;
        width: 100%;
        box-sizing: border-box;
        padding: 10px;
      }
      .receipt-table {
        width: 100%;
        border-collapse: collapse;
        border: 1px solid #000;
        page-break-inside: avoid;
      }
      .receipt-table td {
        border: 1px solid #000;
        padding: 8px;
      }
      .company-title {
        text-align: center;
        font-size: 18px;
        font-weight: bold;
        padding: 8px;
      }
      .receipt-title {
        text-align: center;
        font-size: 16px;
        font-weight: bold;
        padding: 8px;
      }
      .info-cell {
        font-size: 14px;
      }
      .no-cell {
        text-align: right;
        color: #f00;
        font-weight: bold;
      }
      .content-cell {
        padding: 15px 8px;
      }
      .content-row {
        display: flex;
        margin-bottom: 12px;
        line-height: 24px;
      }
      .content-label {
        min-width: 100px;
        font-weight: normal;
      }
      .content-value {
        flex: 1;
      }
      .red-text {
        color: #f00;
      }
      .sign-cell {
        padding: 8px;
      }
      .sign-label {
        margin-right: 10px;
      }
    `;

	doc.open();
	doc.write(`
    <html>
      <head>
        <style>
          @media print {
            ${modeStyles}
            .print-content {
              background: #fff;
            }
            ${config.customStyles}
          }
        </style>
      </head>
      <body>${content}</body>
    </html>
  `);
	doc.close();

	printWindow?.focus();

	setTimeout(() => {
		if (config.showPrintDialog) {
			printWindow?.print();
		} else {
			// 直接打印，不显示对话框（需要浏览器支持）
			const printFunction = printWindow?.document.execCommand
				? () => printWindow.document.execCommand('print', false, null)
				: () => printWindow?.print();
			printFunction();
		}

		if (config.removeAfterPrint) {
			document.body.removeChild(iframe);
		}
	}, 300);

	return {
		printWindow,
		iframe,
		// 提供手动打印方法
		print: () => {
			printWindow?.print();
		},
		// 提供手动关闭方法
		close: () => {
			document.body.removeChild(iframe);
		},
	};
};
